package com.four.controller;

import com.four.pojo.*;
import com.four.service.ICardService;
import com.four.service.IStaffService;
import com.four.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
public class CardController {
    @Autowired
    @Qualifier("cardServiceImpl")
    private ICardService cardServiceImpl;

    @Autowired
    @Qualifier("staffService")
    private IStaffService staffService;

    //查询员工方法
    @RequestMapping("/cardFindAll")
    public String findAll(Model model) {
        List<Staff> list = staffService.findZz();
        model.addAttribute("list", list);
        return "presell_Card.jsp";
    }

    //添加方法
    @RequestMapping("/cardAdd")
    public String cardAdd(Card card) {
        if (card.getClamp().equals("1")) {
            card.setClamp("储值卡");
            if (card.getClamps1().equals("1")) {
                card.setDiscount(9);
                card.setClamps1("都市储值卡");

            }
            if (card.getClamps1().equals("2")) {
                card.setClamps1("白金储值卡");
                card.setDiscount(8);
            }
            if (card.getClamps1().equals("3")) {
                card.setClamps1("红钻储值卡");
                card.setDiscount(7);
            }
        }
        if (card.getClamp().equals("2")) {
            card.setDiscount(9);
            card.setClamp("年卡");
            if (card.getClamps().equals("1")) {
                card.setClamps1("年卡一");
            }
            if (card.getClamps().equals("2")) {
                card.setClamps1("年卡二");
            }
        }
        if (card.getClamps1() == null) {
            card.setClamps1(card.getClamps());
        }
        if (card.getClamps() == null) {
            card.setClamps(card.getClamps1());
        }
        List<Card> cards = cardServiceImpl.findAllCard(card);
        for (int i = 0; i < cards.size(); i++) {
            if (cards.get(i).getCard().equals(card.getCard())) {
                card.setNumber(cards.get(i).getNumber() + card.getMoney());
                cardServiceImpl.updateByPrimaryKeySelective(card);
                return "redirect:/cardFindAll";
            }
        }
        card.setNumber(card.getMoney());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        card.setcTime(date);
        Calendar rightNow = Calendar.getInstance();
        rightNow.add(Calendar.YEAR, 10);// 日期加1年
        card.setEndTime(rightNow.getTime());
        card.setState("可使用");
        card.setStore("太原火车站");
        System.out.println(card);
        cardServiceImpl.insert(card);
        return "redirect:/cardFindAll";
    }

    //营业额详情表
    @RequestMapping("/findAllCard")
    public String findAllCard(Model model, Card card) {
        System.out.println(card);
        if (card.getcTime() == null || "".equals(card.getcTime())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String str = "2021-01-01";
            try {
                card.setcTime(sdf.parse(str));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //findOrdersSum  储值卡消费金额
        int ordersSum = cardServiceImpl.findOrdersSum();
        model.addAttribute("ordersSum", ordersSum);
        //年卡消费次数
        int ordersCount = cardServiceImpl.findOrdersCount();
        model.addAttribute("ordersCount", ordersCount);
        //次数
        int countOrder = cardServiceImpl.findCountOrder(card);
        model.addAttribute("countOrder", countOrder);

        //非会员收入详情
        List<Orders> orders = cardServiceImpl.findOrders(card);
        model.addAttribute("orders", orders);
        List<Gift> gift = cardServiceImpl.findAllGift();
        int countGift = 0;
        for (int i = 0; i < gift.size(); i++) {
            Date bt = gift.get(i).getTion_time();
            Date et = card.getcTime();
            if (bt.before(et)) {
                //表示bt小于et
            } else {
                countGift = cardServiceImpl.findCountGift(card);
            }
        }
        //大礼包
        model.addAttribute("countGift", countGift);
        //非会员总收入
        int countOrders = cardServiceImpl.findCountOrders(card);
        model.addAttribute("countOrders", countOrders);
        //充值详情
        //总充值
        int allCount = cardServiceImpl.findAllCount(card);
        model.addAttribute("allCount", allCount);

        //各个卡片情况
        List<Card> count = cardServiceImpl.findCount(card);
        model.addAttribute("count", count);
        //年卡开卡数
        int count1 = cardServiceImpl.findClamp(card);
        model.addAttribute("count1", count1);
        //卡的详情
        List<Card> list = cardServiceImpl.findAllClamps(card);
        int countCard = cardServiceImpl.findCountCard(card);
        model.addAttribute("countCard", countCard);
        model.addAttribute("list", list);
        return "business_Search.jsp";
    }


    @RequestMapping("/selectResult")
    @ResponseBody
    public ResponseMessage findAllCount1() {
        List<Result> result2 = cardServiceImpl.findResult();
        Result result = new Result();
        List<Result> countKD = cardServiceImpl.findCountKD();
        List<Result> countSum = cardServiceImpl.findCountSum();
        List<Result> sum = cardServiceImpl.findSum();
        for (int i = 0; i < result2.size(); i++) {
            if (i == result2.size()-1) {
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date1 = null;
                try {
                    date = sdf.parse(sdf.format(date));
                    date1 = sdf.parse(sdf.format(result2.get(i).getrTime()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                int compareTo = date.compareTo(date1);
                if (compareTo == 0) {
                    for (int j = 0; j < countKD.size() - 1; j++) {
                        result.setRid(result2.get(j).getRid());
                        result.setCounts(countKD.get(j).getCounts());
                        result.setMoney(countSum.get(j).getSums());
                        result.setSums(sum.get(j).getMoney());
                        result.setrTime(date);
                        cardServiceImpl.updateResult(result);
                    }
                } else if (compareTo == 1) {
                    List<Result> countKD1 = cardServiceImpl.findCountKD();
                    List<Result> countSum1 = cardServiceImpl.findCountSum();
                    List<Result> sum1 = cardServiceImpl.findSum();
                    for (int j = 0; j < countKD.size() - 1; j++) {
                        result.setCounts(countKD1.get(j).getCounts());
                        result.setMoney(countSum1.get(j).getSums());
                        result.setSums(sum1.get(j).getMoney());
                        result.setrTime(date);
                        cardServiceImpl.insertKD(result);
                    }
                }
            }
        }
        List<Result2> result1 = cardServiceImpl.findResult2();
        System.out.println(result1);
        return ResponseMessage.success().addObject("result", result1);
    }

}
